using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace TextEditor.TextDocument
{
    public class ZYTextBorderInput : ZYTextInput
    {
        public ZYTextBorderInput()
        {
            this.Width = 6;
            base.myLastElement = new ZYTextBorderInputEnd();
            base.myLastElement.Parent = this;
            base.myChildElements.Clear();
            this.AddLastElement();
            base.myLines.Clear();
            base.LineSpan = -1;
            base.intLeftMargin = 5;
        }

        protected override void AddLastElement()
         {
            if (!base.myChildElements.Contains(base.myLastElement))
            {
                base.myChildElements.Add(base.myLastElement);
            }
        }

        public override void DrawBackGround(ZYTextElement myElement)
        {
            base.myOwnerDocument.View.DrawLine(Color.Black, myElement.Bounds.Left, myElement.Bounds.Top, myElement.Bounds.Right, myElement.Bounds.Top);
            base.myOwnerDocument.View.DrawLine(Color.Black, this.RealLeft, myElement.Bounds.Top, this.RealLeft, myElement.Bounds.Top + myElement.Height - 1);
            base.myOwnerDocument.View.DrawLine(Color.Black, myElement.Bounds.Left, myElement.Bounds.Top + myElement.Bounds.Height - 1, myElement.Bounds.Right, myElement.Bounds.Top + myElement.Bounds.Height - 1);
        }

        public override string GetXMLName()
        {
            return "borderinput";
        }
      
        public override bool RefreshSize()
        {
            int num = 0;
            int height = 0;
            base.myLastElement.RefreshSize();
            foreach (ZYTextElement element in base.myChildElements)
            {
                if (element.Visible)
                {
                    element.RefreshSize();
                    num += element.Width;
                    if (height < element.Height)
                    {
                        height = element.Height;
                    }
                }
            }
            if (height < base.myOwnerDocument.View.DefaultRowHeight())
            {
                height = base.myOwnerDocument.View.DefaultRowHeight();
            }
            this.Width = 4;
            base.Height = height;
            return true;
        }
    }
    public class ZYTextBorderInputEnd : ZYTextElement
    {
        internal string LastTitle = null;

        public string GetTitle()
        {
            if (this.OwnerInput.ChildCount <= 1)
            {
                return this.OwnerInput.DisplayName;
            }
            return this.OwnerInput.Unit;
        }

        public override void HandleEnter()
        {
            ((ZYTextInput)this.Parent).ResetViewState();
            base.myOwnerDocument.RefreshElement(base.myParent);
        }

        public override void HandleLeave()
        {
            ((ZYTextInput)this.Parent).ResetViewState();
            base.myOwnerDocument.RefreshElement(base.myParent);
        }

        public override bool RefreshSize()
        {
            base.Width = 4;
            if (base.Parent != null)
            {
                base.Height = base.Parent.Height;
            }
            else
            {
                base.Height = 16;
            }
            return true;
        }

        public override int Width
        {
            get
            {
                int elementWidth = 0;
                foreach (ZYTextElement element in Parent.ChildElements)
                {
                    if (element.Deleteted)
                    {
                        continue;
                    }
                    if (!(element is ZYTextBorderInputEnd))
                    {
                        elementWidth += element.Width;
                    }

                }
                if (elementWidth < 6)
                    this.Width = 12;
                else
                    this.Width = 4;

                return base.Width;
            }
            set
            {
                base.Width = value;
            }
        }

        public override bool RefreshView()
        {
            base.myOwnerDocument.View.DrawLine(Color.Black, this.RealLeft + this.Width, this.RealTop + 1, this.RealLeft + this.Width, this.RealTop + this.Height - 1);
            int dy = this.Height / 2;
            return true;
        }   

        public void UpdateTitle()
        {
            this.LastTitle = this.GetTitle();
        }

        public ZYTextBorderInput OwnerInput
        {
            get
            {
                return (ZYTextBorderInput)base.Parent;
            }
            set
            {
                base.myParent = value;
            }
        }
    }
}
